﻿using System.Threading.Tasks;
using FytSoa.Common;
using FytSoa.Core.Model.Sw;
using FytSoa.Service.DtoModel;
using FytSoa.Service.Extensions;
using FytSoa.Service.Interfaces;
namespace FytSoa.Service.Implements
{
    /*!
    * 文件名称：Sw_goods服务接口实现
    */
    public class Sw_goodsService : BaseServer<Sw_goods>, ISw_goodsService
    {
        /// <summary>
        /// 上下架操作
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult<string>> UpdateAuditOption(PageParm param)
        {
            var res = new ApiResult<string>() { statusCode = (int)ApiEnum.Error };
            try
            {
                var list = Utils.StrToListString(param.guid);
                //解决orm不能单列更新问题
                var listGoods = Db.Queryable<Sw_goods>().Where(m=>list.Contains(m.Guid)).ToList();
                if (param.audit==0)
                {
                    foreach (var item in listGoods)
                    {
                        item.IsAudit = false;
                    }
                    //下架操作
                    //await Db.Updateable<Sw_goods>(m=>m.IsAudit==false).Where(m=>list.Contains(m.Guid)).ExecuteCommandAsync();
                }
                else
                {
                    foreach (var item in listGoods)
                    {
                        item.IsAudit = true;
                    }
                    //上架操作
                    //await Db.Updateable<Sw_goods>(m => m.IsAudit == true).Where(m => list.Contains(m.Guid)).ExecuteCommandAsync();
                }
                await Db.Updateable(listGoods).ExecuteCommandAsync();
                res.statusCode = (int)ApiEnum.Status;
            }
            catch (System.Exception ex)
            {
                res.message = ex.Message;
            }
            return res;
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult<Page<Sw_goods>>> GetPageGoods(PageParm param)
        {
            var res = new ApiResult<Page<Sw_goods>>() { statusCode=(int)ApiEnum.Error };
            try
            {
                res.data=await Db.Queryable<Sw_goods>()
                    .WhereIF(!string.IsNullOrEmpty(param.key),m=>m.GoodsName.Contains(param.key))
                    .WhereIF(!string.IsNullOrEmpty(param.where), param.where)
                    .WhereIF(param.id!=0, m=>m.ColumnParentGuid.Contains(","+param.id.ToString()+","))
                    .OrderBy(m=>m.Sort,SqlSugar.OrderByType.Desc)
                    .ToPageAsync(param.page, param.limit);
                res.statusCode = (int)ApiEnum.Status;
            }
            catch (System.Exception ex)
            {
                res.message = ex.Message;
            }
            return res;
        }
    }
}